/**
 * 
 */
package com.moioo.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 3400423@qq.com
 *
 */
public class AuthCheckFilter implements Filter{
	private static final String defaultLoginPage = "";
	
	public void destroy() {		
	}

	
	public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		User user = User.get(request);
		String url = request.getServletPath();
		String ext = url.substring(url.lastIndexOf(".")+1);
		String method = request.getParameter("method");
		String action = request.getParameter("action");
		boolean ischeck = true;
		if("/main.do".equals(url)){
			if("logout".equals(method) || "login".equals(method)){
				ischeck = false;
			}
		}
		if(ischeck && "/survey.do".equals(url) && ("stsave".equals(method) || "st".equals(method))){
			ischeck=false;
		}
		if(ischeck){
			if(user == null || !user.isLogin()){
				//response.sendRedirect("main.do?method=login");
				request.getRequestDispatcher("/main.do?method=login").forward(request, response);
				return;
			}
		}
		chain.doFilter(req, res);
	}

	
	public void init(FilterConfig arg0) throws ServletException {

	}

}
